package com.example.exercise.app.user.controller;

import com.example.exercise.analysis.visit.dto.request.UserLoginRequest;
import com.example.exercise.analysis.visit.service.UserLoginService;
import com.example.exercise.core.base.dto.BaseResponse;
import com.example.exercise.app.user.dto.request.LoginRequest;
import com.example.exercise.app.user.dto.request.UserPwdRequest;
import com.example.exercise.app.user.dto.request.UserRequest;
import com.example.exercise.app.user.dto.response.UserLoginInfo;
import com.example.exercise.app.user.mapper.entity.UserEntity;
import com.example.exercise.app.user.service.UserService;
import com.example.exercise.core.util.DateUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
//@Api(description = "用户 -- 登陆、注册、重置、改密码、安全退出")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private UserLoginService userLoginService;

//    @ApiOperation(value = "登录", notes = "登录")
    @RequestMapping(value = "/doLogin", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Object> login(@RequestBody LoginRequest loginRequest, HttpServletRequest req) throws Exception {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        UserLoginInfo userLoginInfo = userService.login(loginRequest.getUserName(), loginRequest.getPassword());
        if (userLoginInfo != null) {
            baseResponse.ok(userLoginInfo);
            UserLoginRequest userLoginRequest = new UserLoginRequest();
            userLoginRequest.setUserId(userLoginInfo.getUserId());
            userLoginRequest.setLoginDay(DateUtil.getDateStr(0));
            userLoginService.insertUserLogin(userLoginRequest);
        } else {
            baseResponse.setCode(1);
            baseResponse.setMsg("用户名或密码不正确");
        }
        return baseResponse;
    }

//    @ApiOperation(value = "注册", notes = "注册")
    @RequestMapping(value = "/register", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Object> register(@RequestBody UserRequest userRequest) {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        Integer flag = userService.register(userRequest);
        if (flag == 1) {
            baseResponse.setCode(1);
            baseResponse.setMsg("用户名已被注册");
        } else if (flag == 0) {
            baseResponse.ok();
        }
        return baseResponse;
    }

//    @ApiOperation(value = "重置密码", notes = "重置密码")
    @RequestMapping(value = "/resetPassword", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Object> resetPassword(@RequestParam String verifyCode, @RequestParam String userName, @RequestParam String password){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int flag = userService.resetPassword(verifyCode,userName, password);
        if(flag == 1){
            baseResponse.setCode(1);
            baseResponse.setMsg("用户名不正确");
        }else if(flag == 2){
            baseResponse.setCode(2);
            baseResponse.setMsg("校验码错误");
        }else{
            baseResponse.setCode(3);
            baseResponse.setMsg("重置成功");
        }
        return baseResponse;
    }

//    @ApiOperation(value = "修改个人邮箱", notes = "修改个人邮箱")
    @RequestMapping(value = "/updateEmail", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Object> updateEmail(@RequestParam String verifyCode, @RequestParam String email, @RequestParam Integer userId) {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int flag = userService.updatePersonEmail(verifyCode,userId,email);
        if(flag == 0){
            baseResponse.ok();
        }else if(flag == 1){
            baseResponse.setCode(1);
            baseResponse.setMsg("校验码错误");
        }else if(flag == 2){
            baseResponse.setCode(2);
            baseResponse.setMsg("邮箱已被使用");
        }
        return baseResponse;
    }

//    @ApiOperation(value = "修改密码", notes = "修改密码")
    @RequestMapping(value = "/updatePassword", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Object> updatePassword(@RequestBody UserPwdRequest userPwdRequest, @RequestParam Integer userId) {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        boolean bool = userService.updatePassword(userId,userPwdRequest.getPassword(),userPwdRequest.getNewPassword());
        if(bool){
            baseResponse.ok();
        }else{
            baseResponse.setCode(1);
            baseResponse.setMsg("原始密码不正确");
        }
        return baseResponse;
    }

//    @ApiOperation(value = "安全退出", notes = "安全退出")
    @RequestMapping(value = "/logout", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Object> logout(@RequestParam Integer userId) {
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        baseResponse.ok();
        return baseResponse;
    }

//    @ApiOperation(value = "查询个人信息", notes = "查询个人信息")
    @RequestMapping(value = "/findByUserId", method = {RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<UserEntity> findByUserId(@RequestParam Integer userId) {
        BaseResponse<UserEntity> baseResponse = new BaseResponse<>();
        UserEntity userEntity = userService.findByUserId(userId);
        if (userEntity != null) {
            userEntity.setPassword(null);
            baseResponse.ok(userEntity);
        } else {
                baseResponse.setCode(1);
                baseResponse.setMsg("用户名不正确");
            }
        return baseResponse;
    }

//    @ApiOperation(value = "修改个人信息", notes = "修改个人信息")
    @RequestMapping(value = "/modifyUserInfo", method = {RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<UserEntity> modifyUserInfo(UserEntity userEntity) {
        BaseResponse<UserEntity> baseResponse = new BaseResponse<>();
        userService.updatePersonInfo(userEntity);
        baseResponse.ok(userEntity);
        return baseResponse;
    }
}
